def binary_search_iterative(arr, target):
    left, right = 0, len(arr) - 1

    while left <= right:
        mid = (left + right) // 2   # 计算中间索引，//表示整数除法，结果为整数
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

# 示例用法
if __name__ == "__main__":
    # 测试数据（必须是有序的）
    data = [1, 2, 3, 5, 8, 9, 12]
    # data = [8, 9, 12]
    target1 = 8

    # 测试查找目标值8
    result1 = binary_search_iterative(data, target1)
    print(result1)
